package utility.com;

import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import utility.NameValueList;

public class ComponentUtils {

    public ComponentUtils() {}
    
    public static DefaultComboBoxModel getDefaultComboBoxModel(List<NameValueList> nameValueList) {
        try {
            DefaultComboBoxModel defaultComboBoxModel = new DefaultComboBoxModel();
            for (NameValueList e : nameValueList) {
                defaultComboBoxModel.addElement(e);
            }
            return defaultComboBoxModel;
        } catch (Exception ex) { throw new RuntimeException(ex); }
    }
    

//    public static Component findNextFocus() {
//        // Find focus owner
//        Component c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
//        Container root = c == null ? null : c.getFocusCycleRootAncestor();
//
//        if (root != null) {
//            FocusTraversalPolicy policy = root.getFocusTraversalPolicy();
//            Component nextFocus = policy.getComponentAfter(root, c);
//            if (nextFocus == null) {
//                nextFocus = policy.getDefaultComponent(root);
//            }
//            return nextFocus;
//        }
//        return null;
//    }
//
//    public static Component findPrevFocus() {
//        // Find focus owner
//        Component c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
//        Container root = c == null ? null : c.getFocusCycleRootAncestor();
//
//        if (root != null) {
//            FocusTraversalPolicy policy = root.getFocusTraversalPolicy();
//            Component prevFocus = policy.getComponentBefore(root, c);
//            if (prevFocus == null) {
//                prevFocus = policy.getDefaultComponent(root);
//            }
//            return prevFocus;
//        }
//        return null;
//    }
    
    class ActionFocusMover implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
            manager.focusNextComponent();
        }
    }

    class ActionTAFocusMover implements KeyListener {
        @Override public void keyTyped(KeyEvent e) {} 
        @Override public void keyReleased(KeyEvent e) {}

        @Override public void keyPressed(KeyEvent e) {
            switch (e.getKeyCode()) { 
                case KeyEvent.VK_TAB: 
                    KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
                    manager.focusNextComponent();
                    break; 
            }
        }
    }
}
